/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Filter;
import com.googlecode.genericdao.search.Search;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.crystalrange.searching.SearchOperatorListitemRenderer;
import org.crystalrange.searching.SearchOperators;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

public class FiscalPeriodSearchController extends GenericForwardComposer {

    protected Listbox sortOperatorPeriod;
    protected Listbox sortOperatorValidFrom;
    protected Listbox sortOperatorValidTo;
    protected Listbox sortOperatorDescription;
    protected Textbox textboxPeriod;
    protected Datebox dateboxValidFrom;
    protected Datebox dateboxValidTo;
    protected Textbox textboxDescription;
    protected Search searchFiscalPeriod;

    public Search getSearchFiscalPeriod() {
        return this.searchFiscalPeriod;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.sortOperatorPeriod.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorPeriod.setItemRenderer(new SearchOperatorListitemRenderer());

        this.sortOperatorValidFrom.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorValidFrom.setItemRenderer(new SearchOperatorListitemRenderer());

        this.sortOperatorValidTo.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorValidTo.setItemRenderer(new SearchOperatorListitemRenderer());

        this.sortOperatorDescription.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorDescription.setItemRenderer(new SearchOperatorListitemRenderer());

        this.searchFiscalPeriod = (Search) this.execution.getAttribute("searchFiscalPeriod");

        if (this.searchFiscalPeriod != null) {
            for (Filter filter : this.searchFiscalPeriod.getFilters()) {
                if (filter.getProperty().equals("period")) {
                    this.restoreOperator(this.sortOperatorPeriod, filter);
                    this.textboxPeriod.setValue(filter.getValue().toString());
                } else if (filter.getProperty().equals("validFrom")) {
                    this.restoreOperator(this.sortOperatorValidFrom, filter);
                    this.dateboxValidFrom.setValue((Date) filter.getValue());
                } else if (filter.getProperty().equals("validTo")) {
                    this.restoreOperator(this.sortOperatorValidTo, filter);
                    this.dateboxValidTo.setValue((Date) filter.getValue());
                } else if (filter.getProperty().equals("description")) {
                    this.restoreOperator(this.sortOperatorDescription, filter);
                    this.textboxDescription.setValue(filter.getValue().toString());
                }
            }
        }

        component.setAttribute("controller", this);
    }

    private void restoreOperator(Listbox listbox, Filter filter) {
        if (filter.getOperator() == Filter.OP_EQUAL) {
            listbox.setSelectedIndex(1);
        } else if (filter.getOperator() == Filter.OP_NOT_EQUAL) {
            listbox.setSelectedIndex(2);
        } else if (filter.getOperator() == Filter.OP_LESS_THAN) {
            listbox.setSelectedIndex(3);
        } else if (filter.getOperator() == Filter.OP_GREATER_THAN) {
            listbox.setSelectedIndex(4);
        } else if (filter.getOperator() == Filter.OP_LESS_OR_EQUAL) {
            listbox.setSelectedIndex(5);
        } else if (filter.getOperator() == Filter.OP_GREATER_OR_EQUAL) {
            listbox.setSelectedIndex(6);
        } else if (filter.getOperator() == Filter.OP_ILIKE) {
            listbox.setSelectedIndex(7);
        }
    }

    public void onClick$buttonOk(Event event) throws Exception {
        this.searchFiscalPeriod.getFilters().clear();

        if (StringUtils.isNotEmpty(this.textboxPeriod.getText())) {
            Listitem item = this.sortOperatorPeriod.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchFiscalPeriod.addFilter(new Filter("period", this.textboxPeriod.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        if (StringUtils.isNotEmpty(this.dateboxValidFrom.getText())) {
            Listitem item = this.sortOperatorValidFrom.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchFiscalPeriod.addFilter(new Filter("validFrom", this.dateboxValidFrom.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        if (StringUtils.isNotEmpty(this.dateboxValidTo.getText())) {
            Listitem item = this.sortOperatorValidTo.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchFiscalPeriod.addFilter(new Filter("validTo", this.dateboxValidTo.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        if (StringUtils.isNotEmpty(this.textboxDescription.getText())) {
            Listitem item = this.sortOperatorDescription.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchFiscalPeriod.addFilter(new Filter("description", this.textboxDescription.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        this.execution.setAttribute("searchFiscalPeriod", this.searchFiscalPeriod);
        this.execution.setAttribute("result", "ok");
        this.self.detach();
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("searchFiscalPeriod", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }
}
